<template>
  <div class="home">
    <van-search v-model="value" placeholder="请输入搜索关键词" />
    <van-swipe
      class="my-swipe"
      :autoplay="3000"
      indicator-color="white"
      lazy-render
    >
      <van-swipe-item v-for="item in banner" :key="item.id">
        <img :src="item.url" style="height: 230px" />
      </van-swipe-item>
    </van-swipe>
    <div class="goods-channel">
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in channel"
          :key="item.id"
          icon="photo-o"
          :text="item.name"
        >
          <van-image :src="item.iconUrl" />
          <span>{{ item.name }}</span>
        </van-grid-item>
      </van-grid>
    </div>
    <div class="home-brand">
      <van-cell title="品牌商直购" is-link value="更多品牌商" />
      <div class="brand-grid">
      <div class="brand-item" v-for="item in homeData.brandList" :key="item.id">
        <van-image :src="item.picUrl" width="80%" />
        <div class="brand-info">
          <span class="brand-name">{{item.name}}</span>
        </div>
      </div>
    </div>
    </div>
    <div class="home-newGoods">
      <van-cell title="新品首发" is-link value="更多新品首发" />
      <div class="content">
        <div class="item" v-for="item in homeData.newGoodsList" :key="item.id">
          <a :href="`#/detail/${item.id}`">
            <img :src="item.picUrl" />
          </a>
          <span class="name">{{ item.name }}</span>
          <span class="price">{{ item.retailPrice }}</span>
        </div>
      </div>
    </div>
    <div class="home-hotGoods">
      <van-cell title="人气推荐" is-link value="更多人气推荐" />
      <div class="content">
        <div class="item" v-for="item in homeData.hotGoodsList" :key="item.id">
          <a :href="`#/detail/${item.id}`">
            <img :src="item.picUrl" />
          </a>
          <div class="info">
            <span class="name">{{ item.name }}</span>
            <span class="brief">{{ item.brief }}</span>
            <div class="price">
              <span class="retail">¥{{ item.retailPrice.toFixed(2) }}</span>
              <span class="counter">¥{{ item.counterPrice.toFixed(2) }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { getHome } from "../api/index";

const value = ref("");
let banner = ref("");
let channel = ref("");
let homeData = reactive({ newGoodsList: [], hotGoodsList: [], brandList: [] });
//没用reactive，因为不能拿一个新的数据覆盖reactive的数据
onMounted(async () => {
  try {
    let { data } = await getHome();
    banner.value = data.banner;
    channel.value = data.channel;
    homeData.newGoodsList = data.newGoodsList;
    homeData.hotGoodsList = data.hotGoodsList;
    homeData.brandList = data.brandList;
  } catch (error) {
    console.log(error);
  }
});
</script>

<style scoped>
.home {
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
  height: 100vh;
  position: relative;
  padding-bottom: 80px;
}
.van-grid-item {
  height: 50px;
}
.van-image {
  display: block;
  width: 20px;
  height: 20px;
  margin: 0 auto;
}
.goods-channel {
  background: white;
  padding-top: 10px;
}
.van-grid-item span {
  display: block;
  font-size: 14px;
  text-align: center;
  margin-top: 5px;
  margin-bottom: 10px;
  color: #333;
}
.home-newGoods {
  background: #fff;
}
.home-newGoods > .content {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.home-newGoods > .content > .item {
  width: 50%;
}
.home-newGoods > .content > .item img {
  width: 180px;
  height: 180px;
}
.home-newGoods > .content > .item > .name {
  padding-left: 20px;
  position: relative;
  bottom: 10px;
  color: rgb(123, 116, 116);
  white-space: nowrap;
  font-size: 14px;
}
.home-newGoods > .content > .item > .price {
  padding-left: 80px;
  position: relative;
  bottom: 10px;
  color: rgb(171, 149, 109);
  font-size: 14px;
}
.my-swipe,
.van-search {
  flex-shrink: 0;
}
.goods-channel,
.home-newGoods,
.home-brand,
.home-hotGoods {
  margin-bottom: 15px;
}
.home-hotGoods {
  background: #fff;
  margin-bottom: 15px;
}

.home-hotGoods > .content {
  display: flex;
  flex-direction: column;
  padding: 0 15px;
}

.home-hotGoods > .content > .item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.home-hotGoods > .content > .item:last-child {
  border-bottom: none;
}

.home-hotGoods > .content > .item img {
  width: 80px;
  height: 80px;
  margin-right: 15px;
  flex-shrink: 0;
}

.home-hotGoods > .content > .item .info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex-grow: 1;
}

.home-hotGoods > .content > .item .name {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
}

.home-hotGoods > .content > .item .brief {
  font-size: 12px;
  color: #999;
  margin: 5px 0;
}

.home-hotGoods > .content > .item .price {
  display: flex;
  align-items: center;
}

.home-hotGoods > .content > .item .retail {
  font-size: 16px;
  color: #b4282d;
  margin-right: 10px;
}

.home-hotGoods > .content > .item .counter {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
}
.home-brand {
  background: #fff;
}
.brand-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  border-collapse: collapse;
  margin: 0 -1px -1px 0;
}

.brand-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px 0;
  border: 1px solid gainsboro;
}

.brand-item .van-image {
  width: 80%;
  height: auto;
  margin-bottom: 10px;
}

.brand-name {
  font-size: 14px;
  color: #333;
}
</style>